<template>
  <div>
    <div class="goods-container" v-infinite-scroll="loadMore" :infinite-scroll-disabled="loading"
      infinite-scroll-distance="20">
      <div class="goods-items" v-for="(value, index) in items" :key="index" :values="value"
        @click="goToDetail(value.goods_id)">
        <div class="goods-img">
          <img :src="'http://127.0.0.1:8089/static' + value.goods_carousel">
          <div class="goods-info">
            <p>{{value.goods_name}}{{value.goods_intro}}</p>
            <div class="goods-price">
              <span class="price">￥{{value.selling_price}}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="loading" v-if="this.goodsItems.length > 20">加载中...</div>
  </div>
</template>

<script>
  export default {
    props: ['goodsItems'],
    data() {
      return {
        goodsList: [],
        items: '',
        loading: true,
        page: 1,
        value: ''
      }
    },
    created() {
      this.getGoodsInfo()
      console.log('goodsItems', this.goodsItems)
      console.log('items:', this.items)
      // this.goodsInfo = this.goodsItems
    },
    watch: {
      goodsItems(newValue, oldValue) {
        console.log('newValue：', newValue)
        this.items = newValue.slice(0, this.page * 10)
      }
    },
    mounted() {
      // this.getGoodsInfo()
      // console.log('items:', this.items)
    },
    methods: {
      goToDetail(goodsID) {
        this.$toast.loading({
          message: '加载中...',
          forbidClick: true
        })
        setTimeout(() => {
          this.$toast.clear()
          this.$router.push({
            name: 'GoodsDetail',
            params: {
              goodsID: goodsID
            }
          })
        }, 400)
      },
      getGoodsInfo() {
        this.items = this.goodsItems.slice(0, this.page * 10)
      },
      loadMore() {
        this.loading = true
        setTimeout(() => {
          this.page++
          this.loading = false
          this.getGoodsInfo()
        }, 1000)
      }
    }
  }
</script>

<style scoped>
  .goods-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-evenly;
    background-color: #F2F2F2;
    padding-top: 5px;
    padding-bottom: 40px;
    margin-bottom: 10px;
    overflow-y: hidden;
  }

  .goods-items {
    padding-bottom: 80px;
    margin-bottom: 9px;
    position: relative;
    width: 46%;
  }

  .goods-items a:link {
    color: #262626;
  }

  .goods-items a:visited {
    color: #262626;
  }

  .goods-items a:hover {
    color: #262626;
  }

  .goods-items a:active {
    color: #262626;
  }

  .goods-img img {
    width: 100%;
    border-radius: 8px 8px 0 0;
  }

  .goods-info {
    height: 80px;
    background-color: white;
    font-size: 12px;
    position: absolute;
    bottom: 4px;
    left: 0;
    right: 0;
    overflow: hidden;
    text-align: center;
    border-radius: 0 0 8px 8px;
  }

  .goods-info p {
    height: 31px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-bottom: 3px;
  }

  .goods-price {
    position: relative;
    padding-left: 0px;
    height: 30px;
    font-size: 15px;
    line-height: 20px;
    text-align: center;
    margin-top: 10px;
  }

  .goods-info .price {
    position: absolute;
    color: orangered;
    left: 20px;
  }

  .loading {
    position: relative;
    bottom: 55px;
    text-align: center;
    font-size: 14px;
  }
</style>
